home *** CD-ROM | disk | FTP | other *** search
- //document responisble for controlling all user interaction through DOM manipulation
- //of buttons
-
- var colorSelectors; //JS objects responsible for holding the color selectors
- var ids //ids for color selector objects
- var curColor;
- var curRow;
- var selectedDom;
- var colorPngs;
- var curGuess;
- var resultPegs;
- var okButton;
- var allowDupes;
- var allowBlanks;
-
- var code;
- var resultPngs;
- var losses;
- var guesses;
- var wins;
-
- function setup()
- {
- //load prefs
-
- allowDupes = widget.preferenceForKey("allowDupes");
- allowBlanks = widget.preferenceForKey("allowBlanks");
- if(!allowDupes){
- allowDupes = false;
- }
- if(!allowBlanks){
- allowBlanks = false;
- }
- losses = widget.preferenceForKey("losses");
- if(!losses){
- losses = 0;
- widget.setPreferenceForKey(0, "losses");
- }
- guesses = widget.preferenceForKey("guesses");
- if(!guesses){
- guesses = 0;
- widget.setPreferenceForKey(0, "guesses");
- }
- wins = widget.preferenceForKey("wins");
- if(!wins){
- wins = 0;
- widget.setPreferenceForKey(0, "wins");
- }
- document.getElementById("allowBlanks").checked = allowBlanks;
- document.getElementById("allowDupes").checked = allowDupes;
-
- //create buttons, glass and info
- new AppleGlassButton(document.getElementById("doneButton"), "Done", hidePrefs);
- new AppleGlassButton(document.getElementById("newGame"), "New Game", newGame);
- new AppleGlassButton(document.getElementById("hideInstructions"), "Back", hideInstructions);
- new AppleGlassButton(document.getElementById("clearStats"), "Clear Stats", clearStats);
- new AppleInfoButton(document.getElementById("infoButton"), document.getElementById("front"), "white", "white",showPrefs);
-
- //init static arrays
- colorPngs = [ "images/white.png", "images/blue.png", "images/black.png", "images/yellow.png", "images/red.png", "images/green.png" ];
- curGuess = new Array(4);
- colorSelectors = new Array(6);
- curColor = -1;
-
- //create color elements
- ids = [ "white-sel", "blue-sel", "black-sel", "yellow-sel", "red-sel", "green-sel" ];
- for(var i = 0; i < 6; i++){
- var next = document.createElement("img");
- next.setAttribute("id", ids[i]);
- next.setAttribute("src", "images/blank.png");
- next.setAttribute("onclick", "colorSelected(" + i +",this);");
- next.setAttribute("onmouseover", "colorMouseover(this);");
- next.setAttribute("onmouseout", "colorMouseout(this);");
- document.getElementById("front").appendChild(next);
- }
- updateStats();
- setupGame();
- document.addEventListener("keydown", keyHandler, true);
- }
-
- function newGame(){
- hidePrefs();
- setupGame();
- }
-
- function setupGame(){
- //init game specific arrays
- resultPegs = new Array(10);
- for(var i = 0; i < 10; i++){
- resultPegs[i] = new Array(4);
- }
-
- for(var i = 0; i < 4; i++){
- curGuess[i] = -1;
- }
-
- //init game specific variabls
- curRow = 9;
-
- //delete any pre-existing DOM elements
- var toDelete;
- var front = document.getElementById("front");
- if((toDelete = document.getElementById("gameBoard"))){
- front.removeChild(toDelete);
- }
- if((toDelete = document.getElementById("resultPegs"))){
- front.removeChild(toDelete);
- }
- if((toDelete = document.getElementById("okButton"))){
- front.removeChild(toDelete);
- }
- if((toDelete = document.getElementById("flipped"))){
- front.removeChild(toDelete);
- }
-
- //create game board
- var gameBoard = document.createElement("div");
- gameBoard.setAttribute("id", "gameBoard");
- var resultPegsDom = document.createElement("div");
- resultPegsDom.setAttribute("id", "resultPegs");
- //gameBoard.style.position = "absolulte";
- gameBoard.style.left = "57px";
- gameBoard.style.top = "54px";
- var xOffset = 20;
- var yOffset = 26;
- for(var row = 0; row < 10; row++){
- for(var col = 0; col < 4; col++){
- var peg = document.createElement("img");
- //set its position, and style
- peg.style.position = "absolute";
- peg.style.left = 56 + col*yOffset + "px";
- peg.style.top = 53 + row*xOffset + "px";
- peg.setAttribute("src", "images/blank.png");
- peg.setAttribute("class", "peg");
- //set its functionallity
- peg.setAttribute("onclick", "pegClick(" + row + ", " + col + ", this);");
-
- gameBoard.appendChild(peg);
-
- }
- //create result pegs
- var curPeg = (resultPegs[row][0] = document.createElement("img"));
- curPeg.style.position = "absolute";
- curPeg.style.left = "20px";
- curPeg.style.top = 53 + row*xOffset + "px";
- curPeg.setAttribute("src", "images/blank.png");
- resultPegsDom.appendChild(curPeg);
- var curPeg = (resultPegs[row][1] = document.createElement("img"));
- curPeg.style.position = "absolute";
- curPeg.style.left = "28px";
- curPeg.style.top = 53 + row*xOffset + "px";
- curPeg.setAttribute("src", "images/blank.png");
- resultPegsDom.appendChild(curPeg);
- var curPeg = (resultPegs[row][2] = document.createElement("img"));
- curPeg.style.position = "absolute";
- curPeg.style.left = "20px";
- curPeg.style.top = 61 + row*xOffset + "px";
- curPeg.setAttribute("src", "images/blank.png");
- resultPegsDom.appendChild(curPeg);
- var curPeg = (resultPegs[row][3] = document.createElement("img"));
- curPeg.style.position = "absolute";
- curPeg.style.left = "28px";
- curPeg.style.top = 61 + row*xOffset + "px";
- curPeg.setAttribute("src", "images/blank.png");
- resultPegsDom.appendChild(curPeg);
- }
- document.getElementById("front").appendChild(gameBoard);
- document.getElementById("front").appendChild(resultPegsDom);
-
- //set up OK button
- okButton = document.createElement("img");
- okButton.setAttribute("id", "okButton");
- okButton.setAttribute("src", "images/ok.png");
- okButton.setAttribute("onclick", "nextGuess();");
- okButton.style.position = "absolute";
- okButton.style.left = "152px";
- setControlPosition(okButton);
- document.getElementById("front").appendChild(okButton);
-
- setupColorLogic();
- }
-
- function colorSelected(index, domEl){
- curColor = index;
- if(selectedDom != null){
- selectedDom.setAttribute("src", "images/blank.png");
- }
- selectedDom = domEl;
- domEl.setAttribute("src", "images/peg_sel.png");
- }
-
- function colorMouseover(domEl){
- if(selectedDom != domEl){
- domEl.setAttribute("src", "images/peg_hl.png");
- }
- }
-
- function colorMouseout(domEl){
- if(selectedDom != domEl){
- domEl.setAttribute("src", "images/blank.png");
- }
- }
-
- function pegClick(row,col,domEl){
- if(row == curRow){
- if(curGuess[col] == curColor){
- curGuess[col] = -1;
- domEl.setAttribute("src", "images/blank.png");
- }else{
- curGuess[col] = curColor;
- domEl.setAttribute("src", colorPngs[curColor]);
- }
- }
- }
-
- function setControlPosition(domEl){
- domEl.style.top = 52 + curRow*20 + "px";
- }
-
- function nextGuess(){
- evaluate(curGuess);
- if(--curRow < 0){
- gameOver(false);
- return;
- }
- for(var i = 0; i < 4; i++){
- curGuess[i] = -1;
- }
- setControlPosition(okButton);
- }
-
- function keyHandler(e){
- e = (e) ? e : ((window.event) ? event : null);
- if(e){
- var code = (e.charCode) ? e.charCode : ((e.which) ? e.which : e.keyCode);
- keyHit = code - 49;
- if (keyHit < 6 && keyHit > -1) {
- colorSelected(keyHit, document.getElementById(ids[keyHit]));
- }
- e.stopPropagation();
- e.preventDefault();
- }
- }
-
- function clicked(url){
- if(widget)
- widget.openURL(url);
- }
-
- function donate(){
- clicked("https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=backsmith%40gmail%2ecom&item_name=CodeBreaker%20widget&no_shipping=2&no_note=1&tax=0¤cy_code=USD&lc=US&bn=PP%2dDonationsBF&charset=UTF%2d8");
- }
-